﻿using System.Collections.Generic;
using System.Linq;
using Model.Entities;
using Model.Repositories;

namespace Presenter.Users
{
    public class UserInterestsPresenter
    {
        private IUserInterestsView _view;

        public UserInterestsPresenter(IUserInterestsView view, int id)
        {
            _view = view;

            _view.GetInterests(GetInterests(id));
            _view.EntityDeleted += ViewEntityDeleted;
            _view.EntityCreated += ViewEntityCreated;
        }

        void ViewEntityCreated(string email, string password, string title, string parentTitle)
        {
            var obj = RepositoryManager.Instance.UserRepository.GetByEmailAndPassword(email, password);
            if (obj != null && RepositoryManager.Instance.InterestRepository.GetByTitle(title) == null)
            {
                var parent = RepositoryManager.Instance.InterestRepository.GetByTitle(parentTitle);
                    if (parent != null)
                        obj.Interests.Add(new Interest
                                              {
                                                  Title = title,
                                                  Parent = parent
                                              });
                    else
                        obj.Interests.Add(new Interest
                                              {
                                                  Title = title
                                              });
                RepositoryManager.Instance.UserRepository.Update(obj);
            }
        }

        void ViewEntityDeleted(string email, string password, int id)
        {
            var obj = RepositoryManager.Instance.UserRepository.GetByEmailAndPassword(email, password);
            if (obj != null)
            {
                obj.Interests.Remove(obj.Interests.FirstOrDefault(o => o.Id == id));
                RepositoryManager.Instance.UserRepository.Update(obj);
            }
        }

        public UserInterestsPresenter(IUserInterestsView view, string email, string password)
        {
            _view = view;

            _view.GetInterests(GetInterests(email, password));
            _view.EntityDeleted += ViewEntityDeleted;
            _view.EntityCreated += ViewEntityCreated;
        }

        public void Reload(IUserInterestsView view, int id)
        {
            _view = view;

            _view.GetInterests(GetInterests(id));
            _view.EntityDeleted += ViewEntityDeleted;
            _view.EntityCreated += ViewEntityCreated;
        }

        public void Reload(IUserInterestsView view, string email, string password)
        {
            _view = view;

            _view.GetInterests(GetInterests(email, password));
            _view.EntityDeleted += ViewEntityDeleted;
            _view.EntityCreated += ViewEntityCreated;
        }

        Dictionary<int, string> GetInterests(int id)
        {
            var result = new Dictionary<int, string>();

            var obj = RepositoryManager.Instance.UserRepository.Get(id);
            if (obj != null)
            {
                foreach (var interest in obj.Interests)
                {
                    result.Add(interest.Id, interest.Title);
                }
            }

            return result;
        }

        Dictionary<int, string> GetInterests(string email, string password)
        {
            var result = new Dictionary<int, string>();

            var obj = RepositoryManager.Instance.UserRepository.GetByEmailAndPassword(email, password);
            if (obj != null)
            {
                foreach (var interest in obj.Interests)
                {
                    result.Add(interest.Id, interest.Title);
                }
            }

            return result;
        }
    }
}

